//
//  UIGraphView.h
//  AccuroFit
//
//  Created by Yongfeng Li on 9/10/15.
//  Copyright (c) 2015 Nci Technology Inc. All rights reserved.
//

#import <UIKit/UIKit.h>

#define DEGREES_TO_RADIANS(x) (M_PI * x / 180.0)

typedef enum
{
    GraphViewXLabelTypeVerticle,	// X-Axis labels will be rotated 90 degrees counter-clockwise
    GraphViewXLabelTypeHorizontal, // X-Axis labels are horizontal (watch out that they don't over-lap! You can adjust the font through properties defined in this class)
    GraphViewXLabelTypeAngled		// X-Axis labels are rotated 45 degrees counter-clockwise
} GraphViewXLabelType;

typedef enum
{
    GraphViewBarTextTypeRoof,		// Text is on top of the bars
    GraphViewBarTextTypeTop,		// Text is inside the bars at the top
    GraphViewBarTextTypeMiddle		// All text is aligned in the center of the shortest bar
} GraphViewBarTextType;

@class UIGraphView;

@protocol GraphViewDataSource <NSObject>

@required

- (NSUInteger)numberOfBarsInBarChart:(UIGraphView *)barChart;
- (CGFloat)barChart:(UIGraphView *)barChart valueForBarAtIndex:(NSUInteger)index;
- (CGFloat)barChart:(UIGraphView *)barChart xposForBarAtIndex:(NSUInteger)index;
- (UIColor *)barChartColor;

@optional

- (CGFloat)barChart:(UIGraphView *)barChart valueForBdplAtIndex:(NSUInteger)index;

- (CGFloat)barChart:(UIGraphView *)barChart ç:(NSUInteger)index;
- (short)barChart:(UIGraphView *)barChart lPresForBarAtIndex:(NSUInteger)index;
- (short)barChart:(UIGraphView *)barChart hRateForBarAtIndex:(NSUInteger)index;

- (float)barChart:(UIGraphView *)barChart lTimeForBarAtIndex:(NSUInteger)index;
- (float)barChart:(UIGraphView *)barChart hTimeForBarAtIndex:(NSUInteger)index;
//- (float)barChart:(GraphView *)barChart lTimeForBarAtIndex:(NSUInteger)index date:(NSString*)sDate;
// (float)barChart:(GraphView *)barChart hTimeForBarAtIndex:(NSUInteger)index date:(NSString*)sDate;

- (float)barChart:(UIGraphView *)barChart calBurnedForBarAtIndex:(NSUInteger)index;

- (NSString *)barChart:(UIGraphView *)barChart xLabelForBarAtIndex:(NSUInteger)index;
- (NSString *)barChart:(UIGraphView *)barChart textForBarAtIndex:(NSUInteger)index;
- (NSString *)barChart:(UIGraphView *)barChart textForLdpAtIndex:(NSUInteger)index;

- (UIColor *)barChart:(UIGraphView *)barChart colorForBarAtIndex:(NSUInteger)index;

- (CGFloat)barChart:(UIGraphView *)barChart distanceAtIndex:(NSUInteger)index;
- (NSString*)barChart:(UIGraphView *)barChart distTxtForBarAtIndex:(NSUInteger)index;
- (int)goalSteps;
- (float)goalDists;

@end

@protocol GraphViewDelegate <NSObject>

@optional

- (void) animationDidEndForBarChart:(UIGraphView *)barChart;
- (void) moveOtherViewCtrl:(UIGraphView *)pView;
- (void) moveOutForNextOne:(UIGraphView *)pView;

@end

@interface UIGraphView : UIView
{
    __weak id <GraphViewDataSource> _dataSource;
    __weak id <GraphViewDelegate> _delegate;
    
    double _barHeightRatio;
    double _barWidthRatio;
    
    Boolean _bDrawCurve;
    NSNumber *_maxHeight;
    NSNumber *_minHeight;
    NSNumber *_maxWidth;
    NSNumber *_minWidth;
    CGFloat _xLabelMaxHeight;
    
    NSMutableArray *_barHeights;
    NSMutableArray *_barHoriPos;
    NSMutableArray *_hrtHeights;
    
    NSMutableArray *_barLabels;
    NSMutableArray *_barTexts;
    CGFloat _topValue;
    CGFloat _rightValue;
    NSInteger _numberOfBars;
    
    // Borders
    CALayer *_borderLayer;
    CAShapeLayer *_borderPathLayer;
    
    // Bars
    CALayer *_barLayer;
    
    NSMutableArray *_barPathLayers;
    
    // Grid
    CALayer * _gridLayer;
    CALayer * _gridLayer_x;
    CAShapeLayer *_gridPathLayer;
    CAShapeLayer *_gridPathLayer_x;
    CAShapeLayer *_goalPathLayer;
    
    // Labels
    UIView *_yLabelView;
    UIView *_xLabelView;
    CGFloat _xLabelRotation;
    
    UIView *_barTextView;
}

@property (weak, nonatomic) id <GraphViewDataSource> dataSource;
@property (weak, nonatomic) id <GraphViewDelegate> delegate;

@property (nonatomic, assign) CGFloat animationDuration; // How long the entire drawing will take
@property (nonatomic, assign) UIColor *chartBorderColor;

// Control Bars
@property (nonatomic, assign) CGFloat barWidth; // Space inbetween the bars
@property (nonatomic, assign) CGFloat barAlpha;
@property (nonatomic, assign) CGSize barShadowOffset;
@property (nonatomic, assign) UIColor *barShadowColor;
@property (nonatomic, assign) CGFloat barShadowAlpha;
@property (nonatomic, assign) CGFloat barShadowRadius;

// Control the Y-axis Labels
@property (nonatomic, assign) BOOL hasYLabels;
@property (nonatomic, strong) UIFont *yLabelFont;
@property (nonatomic, strong) UIColor *yLabelColor;

// Control the X-axis Labels
@property (nonatomic, strong) UIFont *xLabelFont;
@property (nonatomic, strong) UIColor *xLabelColor;
@property (nonatomic, assign) GraphViewXLabelType xLabelType;

// Control the text near the bar
@property (nonatomic, strong) UIFont *barTextFont;
@property (nonatomic, strong) UIColor *barTextColor;
@property (nonatomic, assign) GraphViewBarTextType barTextType;

// Control the grids
@property (nonatomic, assign) BOOL hasGrids;
@property (nonatomic, strong) UIColor *gridColor;
@property (nonatomic, assign) NSInteger incrementValue; // Every x draw a grid line (this also controls the height of the bar graph which is calculated to be the ceiling of this value (ex. if incrementValue = 10, the highest bar in the set is 23, then the top of the graph will be 30. Or.. incrementValue = 20 and _maxHeight = 46 then top will be 60. hasGrids only controls the displaying of the grid, not this calculation for the top.
@property (nonatomic, assign) NSInteger incrmtxposValue;

@property (nonatomic, assign) short nCurtUnit;
@property (nonatomic, assign) short nSetUnit;
@property (nonatomic, strong) UILabel *lblMeasUnit;
- (void)setGoalWgtInfo:(float)dWgtVal unit:(short)nUnit;
- (void) setCaloriyUnit:(NSInteger)nIdx;

- (void) reloadDataWithColor:(UIColor *)lColor;
- (NSUInteger) getSwitchCount;

@end
